﻿using System;
using System.Linq;
using System.Windows;
using Prism.Ioc;
using Prism.Unity;
using HostComputerPlatform.Views;
using HostComputerPlatform.ViewModels;
using HostComputerPlatform.Services;
using ModuleBase;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using GeneralMotionControlProject.WPF.Services;
using GeneralMotionControlProject.Vision.Services;
using GeneralMotionControlProject.Common.Services;

namespace HostComputerPlatform
{
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<MDbContext>();
            containerRegistry.Register<IPropertisSettingsProvider, PropertisSettingsProvider>();
            containerRegistry.Register<IDialogHostService, DialogHostService>();
            containerRegistry.RegisterForNavigation<MainWindow, MainWindowViewModel>();
            containerRegistry.RegisterSingleton<CustomModuleManager>();
            containerRegistry.Register<IExecutionFlowService, ExecutionFlowService>();
            containerRegistry.RegisterSingleton<ExecutionFlowService>();
            containerRegistry.RegisterSingleton<ModuleStateService>();
            containerRegistry.RegisterSingleton<IAxisCardService, AxisCardService>();
            containerRegistry.RegisterSingleton<IModuleResultService, ModuleResultService>();
            containerRegistry.RegisterDialog<SystemDialogView, SystemDialogViewModel>();
            containerRegistry.RegisterSingleton<IDrawROI, DrawROI>("calibDrawROI");
            containerRegistry.RegisterDialog<CameraCalibDialog, CameraCalibDialogViewModel>();

            {
                containerRegistry.Register<IModuleBase, AxisMoveModel.AxisMoveModule>();
                containerRegistry.Register<IModuleBase, AxisMovePoint.AxisMovePoint>();
                containerRegistry.Register<IModuleBase, InitAxis.InitAxis>();
                containerRegistry.Register<IModuleBase, ModbusCon.ModbusCon>();
                containerRegistry.Register<IModuleBase, AxisHome.AxisHome>();
                containerRegistry.Register<IModuleBase, AxisSerOn.AxisSerOn>();
                containerRegistry.Register<IModuleBase, GetPulseValue.GetPulseValue>();
                containerRegistry.Register<IModuleBase, AxisStop.AxisStop>();
                containerRegistry.Register<IModuleBase, CameraCon.CameraCon>();
                containerRegistry.Register<IModuleBase, GetDi.GetDi>();
                containerRegistry.Register<IModuleBase, GTSCardSetDo.GTSCardSetDo>();
                containerRegistry.Register<IModuleBase, ThreadSleep.ThreadSleep>();
                containerRegistry.Register<IModuleBase, FlyGrabAction.FlyGrabAction>();
            }

            LoadDynamicModules(containerRegistry);
        }
        protected override void OnInitialized()
        {
            base.OnInitialized();
            var mainWindowViewModel = Container.Resolve<MainWindowViewModel>();
        }

        private void LoadDynamicModules(IContainerRegistry containerRegistry)
        {
            var mainWindowViewModel = Container.Resolve<MainWindowViewModel>();
            var modulePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Modules");
            if (!Directory.Exists(modulePath))
            {
                Directory.CreateDirectory(modulePath);
            }

            foreach (var dll in Directory.GetFiles(modulePath, "*.dll"))
            {
                try
                {
                    var assembly = Assembly.LoadFrom(dll);
                    foreach (var type in assembly.GetTypes())
                    {
                        if (typeof(IModuleBase).IsAssignableFrom(type) && !type.IsAbstract)
                        {
                            // 注册模块类型
                            containerRegistry.Register(typeof(IModuleBase), type, type.Name);
                            MyFunc.AddMessage($"成功加载模块: {type.Name}");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MyFunc.AddMessage($"加载模块失败: {dll}, 错误: {ex.Message}");
                }
            }
        }
    }
}